﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace kpi.KpiFileManager
{
    public partial class SalKpiFileMgr : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                String UserAction = Request["ua"];
                String MgrType = Request["mt"];
                String DeleteFileName = Request["f"];

                if (UserAction == null & MgrType == null)
                {
                    if (User.IsInRole("User"))
                    {
                        Response.Redirect("SalKpiFileMgr.aspx?ua=d");
                    }
                    else if (User.IsInRole("salesManager"))
                    {
                        Response.Redirect("SalKpiFileMgr.aspx?ua=m");
                    }
                }
                else if (UserAction == "d")
                {
                    if (User.IsInRole("User"))
                    {
                        ShowFileDownloadLink();
                    }
                }
                else if (UserAction == "m")
                {
                    if (User.IsInRole("salesManager"))
                    {
                        ShowFileMgrLink();
                    }
                    else if (User.IsInRole("master"))
                    {
                        ShowFileMgrLink();
                    }
                }
                else if (MgrType == "df" & DeleteFileName != null)
                {
                    if (User.IsInRole("salesManager"))
                    {
                        DoFileDelete(DeleteFileName);
                        ShowFileMgrLink();
                    }
                    else if (User.IsInRole("master"))
                    {
                        DoFileDelete(DeleteFileName);
                        ShowFileMgrLink();
                    }
                }

                //if ((!User.IsInRole("User")) & (!User.IsInRole("u_manager")) & (!User.IsInRole("Financial")))
                //{
                //    Label_UserGuide.Text = "<br />";
                //    Label_UserGuide.Text += "<fieldset>";
                //    Label_UserGuide.Text += "<legend>说明</legend>";
                //    Label_UserGuide.Text += "<ol>";
                //    Label_UserGuide.Text += "<li>新用户首次使用该系统，首先应当注册用户账户；已经注册账户的用户，请进行登录。</li>";
                //    Label_UserGuide.Text += "<li>系统中的账户只分为“管理者账户”和“一般账户”两种，区别是“管理者账户”可以对所管理的“一般账户”进行查看和评分。</li>";
                //    Label_UserGuide.Text += "<li>所有账户注册完成即为“一般账户”，“管理者账户”需要系统管理员进行<a href=\"Account/UserRolesMgr.aspx\">人工指定</a>。</li>";
                //    Label_UserGuide.Text += "</ol>";
                //    Label_UserGuide.Text += "</fieldset>";
                //    Label_UserGuide.Visible = true;
                //}
                //else if (User.IsInRole("User"))
                //{
                //    Label_NavigationLink.Text = "<br />";
                //    Label_NavigationLink.Text += "<ul>";
                //    Label_NavigationLink.Text += "<li><a href=\"KpiForm_user/KpiForm.aspx\">录入并确认我的KPI信息</a></li>";
                //    //取消KPI自评分
                //    //Label_NavigationLink.Text += "<li><a href=\"KpiForm_user/KpiFormUserRec.aspx\">对我的KPI信息进行自评分</a></li>";
                //    Label_NavigationLink.Text += "<li><a href=\"KpiForm_user/KpiFormUserRec.aspx\">我的KPI完成情况</a></li>";
                //    Label_NavigationLink.Text += "<li><a href=\"KpiForm_user/ShowKpiInfo.aspx\">查看我的KPI考评得分</a></li>";
                //    Label_NavigationLink.Text += "</ul>";
                //    Label_NavigationLink.Visible = true;

                //    Label_UserGuide.Text = "<br />";
                //    Label_UserGuide.Text += "<fieldset>";
                //    Label_UserGuide.Text += "<legend>说明</legend>";
                //    Label_UserGuide.Text += "<ol>";
                //    Label_UserGuide.Text += "<li>您已作为“一般账户”登录。</li>";
                //    Label_UserGuide.Text += "<li>您可以录入自己的KPI信息，录入和修改KPI完成情况，查看自己的KPI得分。</li>";
                //    Label_UserGuide.Text += "<li>您不能查看或管理下级用户。</li>";
                //    Label_UserGuide.Text += "<li>您不能查看或管理其他用户的KPI信息。</li>";
                //    Label_UserGuide.Text += "</ol>";
                //    Label_UserGuide.Text += "</fieldset>";
                //    Label_UserGuide.Visible = true;
                //}
                //else if (User.IsInRole("Financial"))
                //{
                //    Label_NavigationLink.Text = "<br />";
                //    Label_NavigationLink.Text += "<ul>";
                //    Label_NavigationLink.Text += "<li><a href=\"KpiForm_user/KpiForm.aspx\">录入并确认我的KPI信息</a></li>";
                //    Label_NavigationLink.Text += "<li><a href=\"KpiForm_user/KpiFormUserRec.aspx\">对我的KPI信息进行自评分</a></li>";
                //    Label_NavigationLink.Text += "<li><a href=\"KpiForm_user/ShowKpiInfo.aspx\">查看我的KPI考评得分</a></li>";
                //    Label_NavigationLink.Text += "<li><a href=\"KpiForm_uManager/KpiFormUserMgr.aspx\">管理我的下级用户</a></li>";
                //    Label_NavigationLink.Text += "<li><a href=\"KpiForm_uManager/KpiForm.aspx\">管理下级KPI信息</a></li>";
                //    Label_NavigationLink.Text += "</ul>";
                //    Label_NavigationLink.Visible = true;

                //    Label_UserGuide.Text = "<br />";
                //    Label_UserGuide.Text += "<fieldset>";
                //    Label_UserGuide.Text += "<legend>说明</legend>";
                //    Label_UserGuide.Text += "<ol>";
                //    Label_UserGuide.Text += "<li>您已作为“管理者账户”登录。</li>";
                //    Label_UserGuide.Text += "<li>您可以录入自己的KPI信息，对KPI信息进行自评分，查看自己和所属下级的KPI信息。</li>";
                //    Label_UserGuide.Text += "<li>您还可以添加下级用户，并对其KPI信息进行考评。</li>";
                //    Label_UserGuide.Text += "</ol>";
                //    Label_UserGuide.Text += "</fieldset>";
                //    Label_UserGuide.Visible = true;
                //}
            }
        }

        protected void ShowFileDownloadLink()
        {
            String FilePath = MapPath("~/KpiFileManager/Files");

            DirectoryInfo dir = new DirectoryInfo(FilePath);
            FileInfo[] files = dir.GetFiles("*.*");

            string[] FilesArray = new string[files.Length];
            int i = 0;

            Label_NavigationLink.Text = "<br />";
            Label_NavigationLink.Text += "<ul>";

            foreach (FileInfo fileInfo in files)
            {
                FilesArray[i] = fileInfo.Name;

                //Label_NavigationLink.Text += "<li><a href=\"KpiForm_user/KpiForm.aspx\">录入并确认我的KPI信息</a></li>";
                //DropDownList_ImgNameList.Items.Insert(i + 1, FilesArray[i]);
                Label_NavigationLink.Text += "<li><a href=\"Files/" + FilesArray[i].ToString() + "\">" + FilesArray[i].ToString() + "</a></li>";

                i++;
            }

            Label_NavigationLink.Text += "</ul>";
            Label_NavigationLink.Visible = true;
        }

        protected void ShowFileMgrLink()
        {
            FileUpload_SalesFile.Visible = true;
            Button_SalesFileUpload.Visible = true;

            String FilePath = MapPath("~/KpiFileManager/Files/");

            DirectoryInfo dir = new DirectoryInfo(FilePath);
            FileInfo[] files = dir.GetFiles("*.*");

            string[] FilesArray = new string[files.Length];
            int i = 0;

            Label_NavigationLink.Text = "<br />";
            Label_NavigationLink.Text += "<ul>";

            foreach (FileInfo fileInfo in files)
            {
                FilesArray[i] = fileInfo.Name;

                if (FilesArray[i].ToString().Contains(User.Identity.Name))
                {
                    Label_NavigationLink.Text += "<li><a href=\"Files/" + FilesArray[i].ToString() + "\">" + FilesArray[i].ToString() + "</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"SalKpiFileMgr.aspx?mt=df&f=" + FilesArray[i].ToString() + "\">删除</a></li>";
                }
                else if (User.IsInRole("master"))
                {
                    Label_NavigationLink.Text += "<li><a href=\"Files/" + FilesArray[i].ToString() + "\">" + FilesArray[i].ToString() + "</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"SalKpiFileMgr.aspx?mt=df&f=" + FilesArray[i].ToString() + "\">删除</a></li>";
                }
                else
                {
                    Label_NavigationLink.Text += "<li><a href=\"Files/" + FilesArray[i].ToString() + "\">" + FilesArray[i].ToString() + "</a></li>";
                }

                //Label_NavigationLink.Text += "<li><a href=\"Files/" + FilesArray[i].ToString() + "\">" + FilesArray[i].ToString() + "</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"\" OnClick=\"<% = DoFileDelete(\"" + FilesArray[i].ToString() + "\") %>\">删除</a></li>";
                //Label_NavigationLink.Text += "<li><a href=\"Files/" + FilesArray[i].ToString() + "\">" + FilesArray[i].ToString() + "</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"\" OnClick=\"<% = DoFileDelete(\"" + FilesArray[i].ToString() + "\") %>\">删除</a></li>";

                i++;
            }

            Label_NavigationLink.Text += "</ul>";
            Label_NavigationLink.Visible = true;
        }

        protected void Button_SalesFileUpload_Click(object sender, EventArgs e)
        {
            if (FileUpload_SalesFile.HasFile)
            {
                String FileName = Path.GetFileNameWithoutExtension(FileUpload_SalesFile.FileName) + "_" + DateTime.Now.ToString("yyyy-MM-dd_HHmmss", new CultureInfo("zh-CN")) + "_" + User.Identity.Name + Path.GetExtension(FileUpload_SalesFile.FileName);

                FileUpload_SalesFile.PostedFile.SaveAs(MapPath(("~/KpiFileManager/Files/") + FileName));

                ShowFileMgrLink();
            }
        }

        protected void DoFileDelete(String DeleteFileName)
        {
            //必须是绝对路径（物理路径）
            FileInfo DeleteToFile = new FileInfo(MapPath("~/KpiFileManager/Files/") + DeleteFileName);
            if (DeleteToFile.Exists)
            {
                //调试信息
                //Label_DebugMsg.Text = DeleteToFile.FullName;
                //Label_DebugMsg.Visible = true;
                DeleteToFile.Delete();
            }
        }
    }
}